"""\
Load multiple lines and put them together as if they where a single scan.
"""
import ptycho2 as ptycho
from pyE17 import io
import sys

first_scan = 1712
last_scan = 1728

scan_label = 'S%05d_S%05d' % (first_scan, last_scan)

base_path = '/data/CDI/cSAXS_sxdm_2010_02_partial_coherence'
fpattern = base_path + '/analysis/S%05d/S%05d_data_128x128.h5'

# Load all data as dataScan objects
ds_all = [ptycho.experiment.DataScan(fpattern % (i,i)) for i in range(first_scan, last_scan+1)]

# The "out" scan will be based on one of these
ds = ptycho.experiment.DataScan(fpattern % (first_scan,first_scan))

# Now modify the structure
# First combine data
ds.data = np.concatenate([di.data for di in ds_all])
ds.scan_info.shape = ds.data.shape
ds.scan_info.scan_label = scan_label

# Edit positions
step_size_y = .25e-6
positions = np.concatenate([ds_all[i].scan_info.positions_theory + [0., i*step_size_y] for i in range(len(ds_all))])[:,-1::-1]
ds.scan_info.positions = positions
ds.scan_info.positions_theory = positions

ds.scan_info.data_filename = base_path + '/analysis/%s/%s_data_128x128.h5' % (scan_label, scan_label)

# little bug that needs to be solved
ds.mask = ds.fmask
ds.save()
